﻿using System;
using System.Collections.Generic;
using System.Web;
using ExpertMIS.Brokers;
using System.Text;
using System.Web.Services;
using ExpertMIS.Entities;
using SYS.Common.RMSIntf;
using System.Web.SessionState;

namespace ExpertMIS
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class LoginAction1 : IHttpHandler, IRequiresSessionState
    {
        private ADMUserBroker _userBroker = new ADMUserBroker();
        private bool DoLogin(string account, string passwd)
        {
            return _userBroker.VeritifyUser(account, passwd);
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (DoLogin(context.Request["account"], context.Request["passwd"]))
            {
                ADMUserEntity user = _userBroker.GetUserByAccount(context.Request["account"]);
                ExpertMIS.Codes.Utility.GetEnvironment().UserLogon(user as ISYSUser);
                if (true == user.IsAdmin)
                {
                    context.Response.Redirect("Admin/Default.aspx");
                }
                else
                {
                    context.Response.Redirect("Workships/Default.aspx");
                }
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<script type='text/javascript'>");
                sb.Append("alert('输入的用户名或密码有误，请重试！');");
                sb.Append(string.Format("window.location='{0}'", context.Request.UrlReferrer));
                sb.Append("</script>");

                context.Response.Clear();
                context.Response.Write(sb.ToString());
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
